package com.lczyfz.istep.test.testXml;

/**
 * Created by CQH on 2017/4/11.
 */

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.List;

public class testXml {
    public static void main(String[] args) throws Exception {
        String str = "<?xml version='1.0' encoding='UTF-8'?><modes><mode classname=\"module\" class=\"module\" attr_prop_飞哥=\"27\" attr_prop_attri2=\"3\" attr_prop_attri3=\"4\" attr_prop_attri4=\"8\" id=\"12\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/switch-istep.png\" top=\"133\" left=\"246\" zIndex=\"12\" width=\"50\" height=\"50\"/>\n" +
                "<mode classname=\"module\" class=\"module\" attr_prop_飞哥=\"27\" attr_prop_attri2=\"3\" attr_prop_attri3=\"4\" attr_prop_attri4=\"8\" id=\"12\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/switch-istep.png\" top=\"133\" left=\"246\" zIndex=\"12\" width=\"50\" height=\"50\"/>\n" +
                "</modes>";
        String str1="<?xml version='1.0' encoding='utf8'?><modes><mode classname=\"module\" class=\"module\" attr_prop_attri1=\"a\" attr_prop_attri2=\"b\" attr_prop_attri3=\"c\" attr_prop_attri4=\"d\" id=\"14\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/switch-istep.png\" top=\"75\" left=\"507\" zIndex=\"14\" width=\"50\" height=\"50\"/><mode classname=\"module\" class=\"module\" attr_prop_attri1=\"a\" attr_prop_attri2=\"b\" attr_prop_attri3=\"c\" attr_prop_attri4=\"d\" id=\"12\" title=\"\" backImgSrc=\"http://localhost:8080/static/images/topology/switch-istep.png\" top=\"170\" left=\"79\" zIndex=\"12\" width=\"50\" height=\"50\"/><line strokeweight=\"2\" strokecolor=\"black\" brokenType=\"1\" stroke=\"purple\" path=\"M 131 196 L 559 101\" d=\"M 131 196 L 559 101\" id=\"line12\" style=\"cursor: pointer; fill: none; stroke: black; stroke-width: 2;\" marker-end=\"url(#arrow)\" wBaseMode=\"module14\" wIndex=\"5\" xBaseMode=\"module12\" xIndex=\"5\" attr_prop_attri1=\"1\" attr_prop_attri2=\"2\" attr_prop_attri3=\"3\" attr_prop_attri4=\"4\"/></modes>";
        InputStream is = new ByteArrayInputStream(str1.getBytes("gbk"));

//        File file = new File("C:\\Users\\CQH\\Desktop\\testXml2.xml");
//        FileInputStream in = new FileInputStream(file);

        Reader read = new InputStreamReader(is,"gbk");
        SAXReader reader = new SAXReader();
        Document document = reader.read(read);
        Element root = document.getRootElement();
        List<Element> childElements = root.elements();

        for (Element child : childElements) {
       //未知属性名情况下
       List<Attribute> attributeList = child.attributes();
       for (Attribute attr : attributeList) {
        System.out.println("1:"+attr.getName() + ": " + attr.getValue());
       }

       //已知属性名情况下
//     System.out.println("id: " + child.attributeValue("id"));

       //未知子元素名情况下
       List<Element> elementList = child.elements();
       for (Element ele : elementList) {
        System.out.println(ele.getName() + ": " + ele.getText());
       }
       System.out.println();

       //已知子元素名的情况下
//      System.out.println("title" + child.elementText("title"));
//      System.out.println("author" + child.elementText("author"));
         //这行是为了格式化美观而存在
         System.out.println();
        }
    }
}
